#include "saledao.h"

SaleDAO::SaleDAO(Database *db) : m_db(db) {}

bool SaleDAO::addSaleRecord(int productId, int quantity, double price,
                           int customerId, int operatorId, const QString &note)
{
    // 注意：销售记录实际存储在order_main和order_detail表中
    // 此方法可能需要重新设计以配合订单系统
    QString query = "INSERT INTO order_detail (order_id, product_id, quantity, unit_price, subtotal) "
                   "VALUES (?, ?, ?, ?, ?)";
    double subtotal = quantity * price;
    return m_db->execute(query, {0, productId, quantity, price, subtotal}); // order_id需要从外部传入
}

bool SaleDAO::beginTransaction()
{
    return m_db->beginTransaction();
}

bool SaleDAO::commitTransaction()
{
    return m_db->commitTransaction();
}

bool SaleDAO::rollbackTransaction()
{
    return m_db->rollbackTransaction();
}

// 获取所有销售记录（用于统计页面）
QVector<QMap<QString, QVariant>> SaleDAO::getAllSaleRecords()
{
    QString query = "SELECT om.order_id, od.product_id, p.product_name, "
                   "od.quantity, od.unit_price, od.subtotal as total_amount, "
                   "om.order_time as sale_date, om.user_id as operator_id, u.real_name as cashier, "
                   "om.customer_id, c.name as customer_name, "
                   "CASE om.method_id "
                   "  WHEN 1 THEN '现金' "
                   "  WHEN 2 THEN '微信' "
                   "  WHEN 3 THEN '支付宝' "
                   "  WHEN 4 THEN '银行卡' "
                   "  WHEN 5 THEN '会员余额' "
                   "  ELSE '未知' END as payment_method, "
                   "CASE om.order_status "
                   "  WHEN 0 THEN '待支付' "
                   "  WHEN 1 THEN '已支付' "
                   "  WHEN 2 THEN '已完成' "
                   "  WHEN 3 THEN '已取消' "
                   "  WHEN 4 THEN '已退款' "
                   "  ELSE '未知' END as order_status "
                   "FROM order_main om "
                   "INNER JOIN order_detail od ON om.order_id = od.order_id "
                   "LEFT JOIN product p ON od.product_id = p.product_id "
                   "LEFT JOIN user u ON om.user_id = u.user_id "
                   "LEFT JOIN customer c ON om.customer_id = c.customer_id "
                   "WHERE om.order_status = 1 "
                   "ORDER BY om.order_time DESC";
    
    return m_db->query(query);
}

// 根据条件查询销售记录
QVector<QMap<QString, QVariant>> SaleDAO::getSaleRecordsByCondition(
    const QString &startDate, const QString &endDate,
    int productId, int customerId)
{
    QString query = "SELECT om.order_id, od.product_id, p.product_name, "
                   "od.quantity, od.unit_price, od.subtotal as total_amount, "
                   "om.order_time as sale_date, om.user_id as operator_id, u.real_name as cashier, "
                   "om.customer_id, c.name as customer_name, "
                   "CASE om.method_id "
                   "  WHEN 1 THEN '现金' "
                   "  WHEN 2 THEN '微信' "
                   "  WHEN 3 THEN '支付宝' "
                   "  WHEN 4 THEN '银行卡' "
                   "  WHEN 5 THEN '会员余额' "
                   "  ELSE '未知' END as payment_method, "
                   "CASE om.order_status "
                   "  WHEN 0 THEN '待支付' "
                   "  WHEN 1 THEN '已支付' "
                   "  WHEN 2 THEN '已完成' "
                   "  WHEN 3 THEN '已取消' "
                   "  WHEN 4 THEN '已退款' "
                   "  ELSE '未知' END as order_status "
                   "FROM order_main om "
                   "INNER JOIN order_detail od ON om.order_id = od.order_id "
                   "LEFT JOIN product p ON od.product_id = p.product_id "
                   "LEFT JOIN user u ON om.user_id = u.user_id "
                   "LEFT JOIN customer c ON om.customer_id = c.customer_id "
                   "WHERE om.order_status = 1";
    
    QVector<QVariant> params;
    
    if (!startDate.isEmpty()) {
        query += " AND DATE(om.order_time) >= ?";
        params.append(startDate);
    }
    
    if (!endDate.isEmpty()) {
        query += " AND DATE(om.order_time) <= ?";
        params.append(endDate);
    }
    
    if (productId > 0) {
        query += " AND od.product_id = ?";
        params.append(productId);
    }
    
    if (customerId > 0) {
        query += " AND om.customer_id = ?";
        params.append(customerId);
    }
    
    query += " ORDER BY om.order_time DESC";
    
    return m_db->query(query, params);
}